package org.jim.server.command.cs;

import org.jim.core.ImChannelContext;
import org.jim.core.ImPacket;
import org.jim.core.ImStatus;
import org.jim.core.exception.ImException;
import org.jim.core.packets.pub.consts.Command;
import org.jim.core.packets.RespBody;
import org.jim.core.packets.cs.chat.command.CsChatReadReqCmd;
import org.jim.core.utils.JsonKit;
import org.jim.server.JimServerAPI;
import org.jim.server.command.AbstractCmdHandler;
import org.jim.server.command.CommandHandler;
import org.jim.server.protocol.ProtocolManager;

/**
 * 消息阅读指令
 * @author tanyi
 */
@CommandHandler(cmd = Command.COMMAND_CHAT_READ_REQ)
public class CsChatReadReqHandler extends AbstractCmdHandler {
	
	@Override
	public Command command() {
		return Command.COMMAND_CHAT_READ_REQ;
	}

	@Override
	public ImPacket handler(ImPacket packet, ImChannelContext imChannelContext) throws ImException {
		CsChatReadReqCmd chatReadReq;
		try{
			chatReadReq = JsonKit.toBean(packet.getBody(),CsChatReadReqCmd.class);
		}catch (Exception e) {
			//关闭请求消息格式不正确
			return ProtocolManager.Converter.respPacket(new RespBody(Command.COMMAND_CHAT_READ_RESP, ImStatus.C10020), imChannelContext);
		}
		//设置会话已读
		//

		ImPacket chatPacket = new ImPacket(Command.COMMAND_CHAT_READ_RESP, new RespBody(Command.COMMAND_CHAT_READ_RESP, null).toByte());
		//设置同步序列号;
		chatPacket.setSynSeq(packet.getSynSeq());
		JimServerAPI.sendToUser(chatReadReq.getCsId(), chatPacket);
		//发送成功响应包
		RespBody chatDataInCorrectRespPacket = new RespBody(Command.COMMAND_CHAT_READ_RESP, ImStatus.C_OK);
		return ProtocolManager.Converter.respPacket(chatDataInCorrectRespPacket, imChannelContext);
	}

}
